import os
import sys
import tkinter.filedialog as fd


def readFile(filePath):
    try:
        with open(filePath, "rb") as f:
            return f.read()
    except FileNotFoundError as e:
        print(f"读取文件出错，文件不存在：{str(e)}")
        sys.exit(1)
    except Exception as e:
        print(f"读取文件出错：{str(e)}")
        sys.exit(1)


def writeFile(filePath, binData):
    try:
        with open(filePath, "wb") as f:
            f.write(binData)
    except Exception as e:
        print(f"写入文件出错：{str(e)}")
        sys.exit(1)


def selectFile():
    print("请在弹窗中选择文件，若没有找到弹窗，请按Alt+Tab找名为“选择文件”的窗口...")
    filePath = fd.askopenfilename(title="选择文件", initialdir=".")
    if not filePath:
        print("未选择文件！")
        sys.exit(1)
    print(f"文件：{filePath}")
    return filePath
